﻿using DataModels.Dto;
using DataModels.Dto.Vehicle;
using DataModels.Entities.ParkingLot;
using DataModels.Entities.Vehicle;
using DataModels.Enums;
using DataModels.Enums.ParkingLot;
using DataModels.Enums.Vehicle;
using SqlSugar;
using Utils;

namespace Services.Vehicle
{
    public class VehicleService(SqlSugarClient db) : BaseService(db)
    {
        // 获取登录人车辆
        public ResponseModel GetVehicleInfo(Guid ProjectID)
        {
            
            var dt = db.Queryable<VehicleBasic, ParkingSpace, ParkingSpaceUserRelation>((vb,pks,pksur) => new object[]
            {
                JoinType.Left,vb.ParkingSpaceUUID == pks.UUID,
                JoinType.Left,pks.UUID == pksur.ParkingSpaceUUID && pks.Status ==GeneralEnum.Status.Enable && pksur.Status == ParkingSpaceUserRelationStatus.Active
            })
                // 状态正常车辆
                .Where(vb => vb.Status == VehicleStatus.Active)
                .Where(vb => vb.ProjectUUID == ProjectID)
                .Where (vb => vb.UserUUID == UserUUID)
                .Select((vb, pks, pksur) => new { vb, pks, pksur })
                .ToList()
                ;
            List<GetVehicleInfo> res = [.. dt.Select(x => new GetVehicleInfo
            {
                VehicleId = x.vb.UUID,
                LicensePlate = x.vb.LicensePlate,
                StartDate = x.vb.VehicleType == VehicleType.AssignedParking ? x.pksur.StartDate : x.vb.StartDate,
                EndDate = x.vb.VehicleType == VehicleType.AssignedParking ? x.pksur.EndDate : x.vb.EndDate,
                VehicleType = x.vb.VehicleType.GetDescription(),
                CreateTime = x.vb.CreateTime,
                ParkingSpaceId = x.vb.VehicleType == VehicleType.AssignedParking ? x.pks.UUID : null
            })]
                ;

            return Success(res);
        }
    }
}
